package com.md.application.service.api;

import com.md.api.authentication.ApiApi;
import com.md.application.assembler.ApiAssembler;
import com.md.domain.model.bo.QueryApiBo;
import com.md.domain.service.api.ApiDomainService;
import com.md.model.page.PageResponse;
import com.md.model.request.system.api.QueryApiRequest;
import com.md.model.response.system.ApiResponse;
import com.md.model.system.CommonResult;
import com.md.util.ClassUtils;
import com.md.value.constants.RedisCacheConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author 刘骄阳
 * 2022-09-27 18:34
 */
@Service
@RequiredArgsConstructor
public class ApiApplicationService implements ApiApi {
  private final ApiDomainService apiDomainService;

  /**
   * 分页查询接口
   *
   * @param request 请求实体
   * @return 操作结果
   */
  @Cacheable(value = RedisCacheConstants.API_QUERY_API_LIST, key = "#request")
  public CommonResult<PageResponse<ApiResponse>> selectPage(final QueryApiRequest request) {
    QueryApiBo api = ClassUtils.source2Target(request, QueryApiBo.class);
    Optional.ofNullable(request.getModule()).ifPresent(moduleEnum -> api.setModule(moduleEnum.getCode()));
    Optional.ofNullable(request.getAuth()).ifPresent(authEnum -> api.setAuth(authEnum.getCode()));
    return CommonResult.success(ApiAssembler.buildPo2Response(apiDomainService.getRepository().selectPage(api)));
  }
}
